package cn.s1995.sloth.bms.controller;

import java.util.List;
import java.util.Objects;

import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.s1995.sloth.bizs.entity.HotSearch;
import cn.s1995.sloth.bizs.service.HotSearchService;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.excel.annotation.ResponseExcel;
import cn.s1995.sloth.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

/**
 * 热搜
 *
 * @author sloth
 * @date 2023-12-30 09:02:11
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/hotSearch")
@Tag(description = "HotSearch", name = "BMS-热搜管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class HotSearchController {

    private final HotSearchService hotSearchService;

    /**
     * 获取分页
     * @param page 分页对象
     * @param req 热搜
     * @return
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取分页", description = "获取分页")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('bizs_HotSearch_view')")
    public R getPage(@ParameterObject Page page, @ParameterObject HotSearch req) {
		LambdaQueryWrapper<HotSearch> wrapper = buildQueryWrapper(req);
        return R.ok(hotSearchService.page(page, wrapper));
    }

    /**
     * 获取列表
     * @param req 热搜
     * @return
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取列表", description = "获取列表")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('bizs_HotSearch_view')")
    public R getList(@ParameterObject HotSearch req) {
		LambdaQueryWrapper<HotSearch> wrapper = buildQueryWrapper(req);
        return R.ok(hotSearchService.list(wrapper));
    }

    /**
     * 获取对象
     * @param id id
     * @return R
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取对象", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('bizs_HotSearch_view')")
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(hotSearchService.getById(id));
    }

    /**
     * 新增热搜
     * @param req 热搜
     * @return R
     */
	@ApiOperationSupport(order = 10)
    @Operation(summary = "新增热搜", description = "新增热搜")
    @SysLog("新增热搜")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('bizs_HotSearch_add')")
    public R save(@RequestBody HotSearch req) {
        return R.ok(hotSearchService.save(req));
    }

    /**
     * 修改热搜
     * @param req 热搜
     * @return R
     */
	@ApiOperationSupport(order = 20)
    @Operation(summary = "修改热搜", description = "修改热搜")
    @SysLog("修改热搜")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('bizs_HotSearch_edit')")
    public R updateById(@RequestBody HotSearch req) {
        return R.ok(hotSearchService.updateById(req));
    }

    /**
     * 通过id删除热搜
     * @param ids id列表
     * @return R
     */
	@ApiOperationSupport(order = 30)
    @Operation(summary = "通过id删除热搜", description = "通过id删除热搜")
    @SysLog("通过id删除热搜")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('bizs_HotSearch_del')")
    public R remove(@RequestBody Long[] ids) {
        return R.ok(hotSearchService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * 导出excel 表格
     * @param req 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
	@ApiOperationSupport(order = 40)
    @Operation(summary = "导出excel表格", description = "")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('bizs_HotSearch_export')")
    public List<HotSearch> export(HotSearch req, Long[] ids) {
		LambdaQueryWrapper<HotSearch> wrapper = buildQueryWrapper(req);
		wrapper.in(ArrayUtil.isNotEmpty(ids), HotSearch::getId, ids);
        return hotSearchService.list(wrapper);
    }

	private LambdaQueryWrapper<HotSearch> buildQueryWrapper(HotSearch req) {
        LambdaQueryWrapper<HotSearch> wrapper = Wrappers.lambdaQuery();
        wrapper.and(
                StrUtil.isNotEmpty(req.getSearch()),
                w -> w.like(HotSearch::getTitle, req.getSearch())
                        .or()
                        .like(HotSearch::getContent, req.getSearch())
        );
		wrapper.eq(Objects.nonNull(req.getIsShow()), HotSearch::getIsShow, req.getIsShow());
		wrapper.orderByDesc(HotSearch::getSort);
		return wrapper;
	}
}
